﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace XLDL.Includes
{
    public partial class NhomSP_LienQuan : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             Page.MaintainScrollPositionOnPostBack = true;
          //   if (!IsPostBack)
           // {
                 xuat_luoi();
                 if (Session["sx"].ToString() != "")
                 {

                     for (int i = 0; i < DropDownList_sapxep.Items.Count; i++)
                     {
                         if (DropDownList_sapxep.Items[i].Value.ToString() == Session["sx"].ToString())
                         {
                             DropDownList_sapxep.SelectedIndex = i;
                         }
                     }
                 }
           // }

        }
        protected void xuat_luoi()
        {
            if (Session["KeySort"].ToString() == "")
            {
                Session["KeySort"] = " ngay_dang_sp DESC";
            }
            string ID = Request.QueryString["id"].ToString();
            string NID = Request.QueryString["nid"].ToString();
            string id_gt = Request.QueryString["gt"].ToString();
            DataTable tb = new DataTable();
            tb = XLDL.XLDaTa("select * from V_SanPham where Id_Nhom_sp=" + int.Parse(NID) + " and id_sp not in (" + int.Parse(ID) + ") and id_gt=" + id_gt + " ORDER BY " + Session["KeySort"].ToString() + "");
            CollectionPager1.PageSize = 6;
            CollectionPager1.DataSource = tb.DefaultView;
            CollectionPager1.BindToControl = DataList_load;
            DataList_load.DataSource = CollectionPager1.DataSourcePaged;
            // DataList_load.DataSource = tb;
            DataList_load.DataBind();
            CollectionPager1.Visible = true;
            if (tb.Rows.Count !=0)
            {
                lbl_splienquan.Text = "Sản phẩm <a href='index.aspx?act=sanpham&nid=" + tb.Rows[0]["ID_nhom_SP"].ToString() + "&Gt=" + tb.Rows[0]["id_gt"].ToString() + "'>" + tb.Rows[0]["ten_nhom_sp"].ToString().ToLower() + "</a> liên quan (" + tb.Rows.Count.ToString() + ")";
                CollectionPager1.Visible = true;
            }
            else
            {
                lbl_splienquan.Text = "Sản phẩm liên quan (0)";
                CollectionPager1.Visible = false;
            }

            //CollectionPager1.
            // focus nút chitet
            try
            {
               // string Gt = Request.QueryString["gt"].ToString();
                string p = Request.QueryString["p"].ToString();
                if (p != "")
                {
                    ImageButton_abc.Focus();
                }

            }
            catch { }

        }

        protected void DataList_load_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Image img = new Image();
            img = (Image)e.Item.FindControl("Img1");
            Label lb = new Label();
            lb = (Label)e.Item.FindControl("lbl_moi_cu");
            if (lb.Text == "False")
            {
                img.Visible = false;
            }
            // Size
            //Label id = new Label();
            //id = (Label)e.Item.FindControl("lbl_ID_SP"); // id_SP
            //list Size
            //DropDownList drp = new DropDownList();
            //drp = (DropDownList)e.Item.FindControl("drp_so_size");
            //DataTable tb = new DataTable();
            //tb = XLDL.XLDaTa("select * from V_Size where id_sp=" + int.Parse(id.Text) + " order by so_size asc");
            //drp.DataSource = tb;
            //drp.DataTextField = "so_size";
            //drp.DataValueField = "id_size";
            //drp.DataBind();

            // tinh trang

            Label tt = new Label();
            tt = (Label)e.Item.FindControl("lbl_tinh_trang");
            //enable and disable nut mua ngay
            //ImageButton ib = new ImageButton();
            //ib = (ImageButton)e.Item.FindControl("b_mua_ngay");
            if (tt.Text == "0") // 0 ko, 1 ko , 2 sap ve 
            {
                tt.Text = " Hết hàng";
             //   ib.Visible = true;
            }
            else if (tt.Text == "1")
            {
                tt.Text = " Còn hàng";
               // ib.Visible = true;
            }
            else
            {
                tt.Text = " Sắp về";
               // ib.Visible = true;
            }

            // gan id=lbl_id
            


        }

        protected void DataList_load_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "mua_ngay")
            {

                XL_Gio_hang bang_gio_hang = (XL_Gio_hang)Session["gio_hang"];
                int ms = int.Parse(e.CommandArgument.ToString());
                Label gia_sp = (Label)e.Item.FindControl("lbl_Gia_SP");
                HyperLink ten_sp = (HyperLink)e.Item.FindControl("HyperLink_tensp");
                DropDownList so_size = (DropDownList)e.Item.FindControl("drp_so_size");
                //  DataRow dong = new DataRow();
                int d = bang_gio_hang.Rows.Count;
                bool ok = false;

                if (d == 0)
                {
                    DataRow dong_moi = bang_gio_hang.NewRow();
                    dong_moi["STT"] = 1;
                    dong_moi["Id_SP"] = ms;
                    dong_moi["ten_SP"] = ten_sp.Text;
                    dong_moi["so_luong"] = 1;
                    dong_moi["gia_sp"] = double.Parse(gia_sp.Text);
                    dong_moi["so_size"] = so_size.SelectedItem.ToString();
                    bang_gio_hang.Rows.Add(dong_moi);
                }
                else
                {

                    for (int i = 0; i <= bang_gio_hang.Rows.Count - 1; i++)
                    {
                        if (bang_gio_hang.Rows[i]["so_size"].ToString() == so_size.SelectedItem.ToString() && bang_gio_hang.Rows[i]["id_sp"].ToString() == ms.ToString())
                        {
                            int sl = (int)bang_gio_hang.Rows[i]["so_luong"];
                            bang_gio_hang.Rows[i]["so_luong"] = sl + 1;
                            ok = true;
                            break;
                        }
                    }
                    if (ok != true)
                    {
                        DataRow dong_moi = bang_gio_hang.NewRow();
                        dong_moi["STT"] = 1;
                        dong_moi["Id_SP"] = ms;
                        dong_moi["ten_SP"] = ten_sp.Text;
                        dong_moi["so_luong"] = 1;
                        dong_moi["gia_sp"] = double.Parse(gia_sp.Text);
                        dong_moi["so_size"] = so_size.SelectedItem.ToString();




                        bang_gio_hang.Rows.Add(dong_moi);
                    }

                    int stt = 0;
                    for (int i = 0; i <= bang_gio_hang.Rows.Count - 1; i++)
                    {
                        stt += 1;
                        bang_gio_hang.Rows[i]["STT"] = stt;
                    }
                    // cap nhat sTT

                }




                // cap nhat tat ca vao bang
                bang_gio_hang.AcceptChanges();
                Session["gio_hang"] = bang_gio_hang;

                // dem so lan xem



            }
            xuat_luoi();

        }

        protected void DropDownList_sapxep_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList_sapxep.SelectedValue.ToString() == "2")
            {
                Session["KeySort"] = " gia_sp desc";
            }
            else if (DropDownList_sapxep.SelectedValue.ToString() == "3")
            {
                Session["KeySort"] = " gia_sp asc";
            }
            else
            {
                Session["KeySort"] = " ngay_dang_sp DESC";
            }
            Session["sx"] = DropDownList_sapxep.SelectedValue.ToString();
            xuat_luoi();
        }
       
    }
}